﻿




using System.Web.Mvc;
using Hotpot.Extensions;
using Hotpot.Services;
using Hotpot.ViewModels;

namespace Hotpot.Filters
{
    public class LocalizationActionFilter : IActionFilter, IExceptionFilter
    {
        private readonly ILocalizationService locService;

        public LocalizationActionFilter(ILocalizationService locService)
        {
            this.locService = locService;
        }

        #region IActionFilter Members

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ViewResult result = filterContext.Result as ViewResult;

            if (result != null)
                setLocalization(result.ViewData.Model as HotpotViewModel);
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
        }

        #endregion

        #region IExceptionFilter Members

        public void OnException(ExceptionContext filterContext)
        {
            setLocalization(filterContext.Controller.ViewData.Model as HotpotViewModel);
        }

        #endregion

        #region Private Methods

        private void setLocalization(HotpotViewModel model)
        {
            if (model != null)
                model.AddModelItem(locService.GetTranslations());
        }

        #endregion
    }
}
